<template>
  <div class="bg-white m-4 mr-0">
    <BasicTree
      title="分类"
      search
      :treeData="treeData"
      :replaceFields="{ key: 'id', title: 'name' }"
      @select="handleSelect"
    >
      <template #headerTitle>
        <BasicTitle class="w-1/2 flex justify-between"> 分类 </BasicTitle>
      </template>
    </BasicTree>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTitle } from '/@/components/Basic';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import { getListAll } from '/@/api/cuisine/cuisineType';
  import AButton from '/@/components/Button/src/BasicButton.vue';

  export default defineComponent({
    name: 'FeedTypeTree',
    components: { AButton, BasicTree, BasicTitle },

    emits: ['select'],
    setup(_, { emit }) {
      onMounted(() => {
        fetch();
      });

      const treeData = ref<TreeItem[]>([]);

      async function fetch() {
        treeData.value = (await getListAll({ type: 1 })) as unknown as TreeItem[];
      }

      function handleSelect(keys: string, e) {
        emit('select', keys[0]);
        console.log(keys, e);
      }

      function handleSuccess() {
        fetch();
      }

      return { treeData, handleSelect, handleSuccess };
    },
  });
</script>
